﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Casemeet.Articles;
using VK.Comm;
using VK.Casemeet.Employees;

namespace VK.Casemeet.Web.Admin.Articles
{
    public partial class ArticleEdit : System.Web.UI.Page
    {
        public int articleID = 0;
        public string content = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="文章",Url="Index.aspx"},
                new MapPath{ Text="编辑文章",Url="#"}
            };
            
            articleID = Util.GetQueryInt("ArticleID");

            if (!IsPostBack)
            {
                ArticleHelper.BindCategoryDDL(ddlCategoryID, OptionType.Select);
                lbEditor.Text = EmployeeService.GetSessionEmployee().Account;
                LoadData();
            }
        }

        internal void LoadData()
        {
            if (articleID != 0)
            {
                ArticleEntity article = ArticleBLL.GetArticle(articleID);
                ddlCategoryID.SelectedValue = article.CategoryID.ToString();
                txtTitle.Text = article.Title;
                txtAuthor.Text = article.Author;
                txtSource.Text = article.Source;
                content = article.Content;
                chkIsEnable.Checked = article.IsEnable;
                txtCreated.Text = Util.FormatDateTime(article.Created, DateTimeStyle.Null);
                txtHits.Text = article.Hits.ToString();
                txtTag.Text = article.Tag;
                newsPhoto1.ValueUrl = article.ImgUrl;
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            ArticleEntity article = LoadEntity();
            article.ArticleID = articleID;

            if (ArticleBLL.UpdateArticle(article) > 0)
            {
                Message.Msg(this.Page, CS.MsgEditSuccess, MessageType.Success);
            }
            else
            {
                Message.Msg(this.Page, CS.MsgEditFailure, MessageType.Failure);
            }
            LoadData();
        }

        protected ArticleEntity LoadEntity()
        {
            ArticleEntity article = ArticleBLL.GetArticle(articleID);
            article.CategoryID = int.Parse(ddlCategoryID.SelectedValue);
            article.Title = txtTitle.Text;
            article.Author = txtAuthor.Text;
            article.Source = txtSource.Text;
            article.Content = Server.HtmlEncode(Util.GetFormString("txtContent"));
            article.IsEnable = chkIsEnable.Checked;
            article.Created = Util.ConverDateTime(txtCreated.Text);
            article.Updated = Util.ConverDateTime(txtCreated.Text);
            article.Hits = int.Parse(txtHits.Text);
            article.Editor = lbEditor.Text;
            article.Tag = txtTag.Text;
            article.ImgUrl = newsPhoto1.ValueUrl;

            return article;
        }
    }
}